package servlet;

import bean.Result;
import biz.ShopBiz;
import com.google.gson.Gson;
import dao.ShopDao;
import utils.DBHelper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryShopServlet", value = "/QueryShopServlet.s")
public class QueryShopServlet extends HttpServlet {
    private ShopBiz biz = new ShopBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Result result;
        try{

            String phonepage = request.getParameter("phonepage");// 手机的要查询的页数
            String compage = request.getParameter("compage");// 电脑的要查询的页数
            String earpage = request.getParameter("earpage");// 耳机的要查询的页数
            String otherpage = request.getParameter("otherpage");// 配件的要查询的页数


            Map<String, Object> data = new HashMap<>();
            Map<String, Object> pdata = biz.phonequery(phonepage);
            Map<String, Object> cdata = biz.comquery(compage);
            Map<String, Object> edata = biz.earquery(earpage);
            Map<String, Object> odata = biz.otherquery(otherpage);

            data.put("pdata", pdata);// 将手机的总行数和单页数据 存进data
            data.put("cdata", cdata);// 将电脑的总行数和单页数据 存进data
            data.put("edata", edata);// 将耳机的总行数和单页数据 存进data
            data.put("odata", odata);// 将配件的总行数和单页数据 存进data

            //String compage = request.getParameter("compage");// 电脑要查询的页数
            //String type = request.getParameter("type");// 要查询的类型 手机？电脑？配件
            //System.out.println(type);
            //Map<String, Object> data = biz.cardquery(phonepage,type);// 获取biz返回的Map集合 里面包括了总行数 与 查询那页的4行数据
            result = new Result(1, "成功!", data);// 返回前台
        } catch (Exception e){
            e.printStackTrace();
            result = new Result(0, "显示失败!", null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
